home *** CD-ROM | disk | FTP | other *** search
/ The X-Philes (2nd Revision) / The X-Philes Number 1 (1995).iso / xphiles / hp95 / freyja_t.z / freyja_t / ini.c < prev    next >
C/C++ Source or Header  |  1992-04-14  |  6KB  |  298 lines

  1. /* INI.C -- Routines to Load the Initialization File
  2.  
  3.     Written March 1991 by Craig A. Finseth
  4.     Copyright 1991 by Craig A. Finseth
  5. */
  6.  
  7. #include "freyja.h"
  8.  
  9. #define PAGEK        (PAGESIZE / 1024)
  10.  
  11. /* parsing and initialization tables */
  12.  
  13. #define NUM_IC        (sizeof(ic) / sizeof(ic[0]))
  14. static struct ini_char {
  15.     char *tok;    /* token to match */
  16.     char *var;    /* variable to store into */
  17.     char def;    /* default value */
  18.     char *valid;    /* valid characters */
  19.     } ic[] = {
  20. #if defined(UNIX)
  21. { "SCR", &c.g.screen_type,    'V',    "@VT" },    /* must be first */
  22. { "SCS", &c.g.screen_size,    '@',    "@" },
  23. { "KEY", &c.g.key_type,        'C',    "@C" },
  24. { "SPE", &c.g.special,        '@',    "@" },
  25. { "PA1", &c.g.parameter_1,    '@',    "@" },
  26. #else
  27. #if defined(MSDOS)
  28. { "SCR", &c.g.screen_type,    'V',    "@VBM" },    /* must be first */
  29. { "SCS", &c.g.screen_size,    '@',    "@J" },
  30. { "KEY", &c.g.key_type,        'C',    "@CSBJ" },
  31. { "SPE", &c.g.special,        '@',    "@J" },
  32. { "PA1", &c.g.parameter_1,    '@',    "@P" },
  33. #else
  34. { "SCR", &c.g.screen_type,    'V',    "@V" },        /* must be first */
  35. { "SCS", &c.g.screen_size,    '@',    "@" },
  36. { "KEY", &c.g.key_type,        'C',    "@C" },
  37. { "SPE", &c.g.special,        '@',    "@" },
  38. { "PA1", &c.g.parameter_1,    '@',    "@" },
  39. #endif
  40. #endif
  41. { "MET", &c.g.meta_handle,    'B',    "@BIM" },
  42. { "FIL", &c.d.fill,        'N',    "@NFW" } };
  43.  
  44. #define NUM_IF        (sizeof(ifl) / sizeof(ifl[0]))
  45. static struct ini_flag {
  46.     char *tok;    /* token to match */
  47.     FLAG *var;    /* variable to store into */
  48.     FLAG def;    /* default value */
  49.     } ifl[] = {
  50. { "WRA", &c.g.wrap_allowed,    TRUE },
  51. { "VIS", &c.g.vis_gray,        FALSE },
  52. { "USE", &c.g.use_caret,    TRUE },
  53. { "SBU", &c.g.skip_system,    TRUE },
  54. { "SAV", &c.g.saving,        TRUE } };
  55.  
  56. #define NUM_II        (sizeof(ii) / sizeof(ii[0]))
  57. static struct ini_int {
  58.     char *tok;    /* token to match */
  59.     int *var;    /* variable to store into */
  60.     int def;    /* default value */
  61.     } ii[] = {
  62. { "SWA", &c.g.swap_size,    128 },
  63. { "ESC", &c.g.ESC_swap,        27 },
  64. { "CTX", &c.g.CTX_swap,        24 },
  65. { "LAN", &c.g.language,        1 },
  66. { "LEF", &c.d.left_margin,    0 },
  67. { "RIG", &c.d.right_margin,    70 },
  68. { "TAB", &c.d.tab_spacing,    8 } };
  69.  
  70.  
  71. static char *buffer;
  72. static char *bufptr;
  73. static int buflen;
  74. static int line;
  75. static int fd;
  76.  
  77. int Ini_Next();        /* int fd */
  78. FLAG Ini_Token();    /* char *token, int len */
  79.  
  80. /* ------------------------------------------------------------ */
  81.  
  82. /* Load the configuration information.  If no file, use defaults.  If
  83. an error, print a message and use defaults. */
  84.  
  85. FLAG
  86. IniLoad(s, z)
  87.     char s;
  88.     int z;
  89.     {
  90.     char fname[FNAMEMAX];
  91.     char buf[BUFFSIZE];
  92.     char tok1[32];
  93.     char tok2[32];
  94.     char chr;
  95.     int cnt;
  96.     FLAG handled;
  97.  
  98. /* set defaults */
  99.  
  100.     for (cnt = 0; cnt < NUM_IC; cnt++) {
  101.         *ic[cnt].var = ic[cnt].def;
  102.         }
  103.     for (cnt = 0; cnt < NUM_IF; cnt++) {
  104.         *ifl[cnt].var = ifl[cnt].def;
  105.         }
  106.     for (cnt = 0; cnt < NUM_II; cnt++) {
  107.         *ii[cnt].var = ii[cnt].def;
  108.         }
  109.  
  110.     s = xtoupper(s);
  111.     if (*sindex(ic[0].valid, s) == NUL) {
  112. #if defined(SYSMGR)
  113.         xsprintf(buf, "option '%c' not %s",
  114.             s, ic[0].valid);
  115.         JMsg(buf);
  116. #else
  117.         xeprintf("option value '%c' is not one of %s.\r\n",
  118.             s, ic[0].valid);
  119. #endif
  120.         return(FALSE);
  121.         }
  122.     c.g.screen_type = s;
  123.     c.g.swap_size = z;
  124.  
  125. /* process the file */
  126.  
  127.     *fname = NUL;
  128.     if ((fd = FPathOpen(INI_FILENAME, fname)) < 0) goto OK;
  129.  
  130.     buffer = buf;
  131.     bufptr = buffer;
  132.     buflen = -1;
  133.     line = 1;
  134.  
  135.     while (Ini_Token(tok1, sizeof(tok1)) && Ini_Token(tok2, sizeof(tok2))){
  136.         handled = FALSE;
  137.  
  138.         for (cnt = 0; cnt < NUM_IC; cnt++) {
  139.             if (!strequ(tok1, ic[cnt].tok)) continue;
  140.             *ic[cnt].var = xtoupper(*tok2);
  141.             if (*sindex(ic[cnt].valid, *ic[cnt].var) == NUL) {
  142. #if defined(SYSMGR)
  143.                 xsprintf(buf, "%s value '%c' not %s",
  144.                     ic[cnt].tok, *tok2, ic[cnt].valid);
  145.                 JMsg(buf);
  146. #else
  147.                 xeprintf("%s value '%c' is not one of %s.\r\n",
  148.                     ic[cnt].tok, *tok2, ic[cnt].valid);
  149. #endif
  150.                 }
  151.             else    handled = TRUE;
  152.             }
  153.  
  154.         for (cnt = 0; cnt < NUM_IF; cnt++) {
  155.             if (!strequ(tok1, ifl[cnt].tok)) continue;
  156.             chr = xtoupper(*tok2);
  157.             if (*sindex("YN", chr) == NUL) {
  158. #if defined(SYSMGR)
  159.                 xsprintf(buf, "%s value '%c' not Y or N",
  160.                     ifl[cnt].tok, *tok2);
  161.                 JMsg(buf);
  162. #else
  163.                 xeprintf("%s value '%s' is not one of Y or N.\r\n",
  164.                     ifl[cnt].tok, tok2);
  165. #endif
  166.                 }
  167.             else    {
  168.                 *ifl[cnt].var = chr == 'Y';
  169.                 handled = TRUE;
  170.                 }
  171.             }
  172.  
  173.         for (cnt = 0; cnt < NUM_II; cnt++) {
  174.             if (!strequ(tok1, ii[cnt].tok)) continue;
  175.             if (!SToN(tok2, ii[cnt].var, 10)) {
  176. #if defined(SYSMGR)
  177.                 xsprintf(buf, "%s value '%s' not number",
  178.                     ii[cnt].tok, tok2);
  179.                 JMsg(buf);
  180. #else
  181.                 xeprintf("%s value '%s' is not a number.\r\n",
  182.                     ii[cnt].tok, tok2);
  183. #endif
  184.                 }
  185.             else    {
  186.                 if (*ii[cnt].var < 0) *ii[cnt].var = 0;
  187.                 handled = TRUE;
  188.                 }
  189.             }
  190.  
  191.         if (!handled) {
  192. #if defined(SYSMGR)
  193.             xsprintf(buf, "problem on line %d of ini file %s",
  194.                 line, fname);
  195.             JMsg(buf);
  196. #else
  197.             xeprintf("Problem on line %d of ini file %s.\r\n",
  198.                 line, fname);
  199. #endif
  200.             close(fd);
  201.             return(FALSE);
  202.             }
  203.         }
  204.     close(fd);
  205.  
  206. /* handle consistency */
  207.  
  208. OK:
  209.  
  210.     for (cnt = 0; cnt < NUM_IC; cnt++) {
  211.         if (*ic[cnt].var == '@') *ic[cnt].var = ic[cnt].def;
  212.         }
  213.  
  214.     if (c.g.swap_size < 16) c.g.swap_size = 16;
  215.     if (c.g.swap_size > 512) c.g.swap_size = 512;
  216.     c.g.swap_size = (c.g.swap_size + PAGEK - 1) / PAGEK;
  217.     c.g.swap_size *= PAGEK;
  218.  
  219. #if defined(MSDOS)
  220. #if defined(SYSMGR)
  221.     c.g.special = 'J';
  222.     c.g.screen_type = 'M';
  223.     c.g.screen_size = 'J';
  224.     c.g.key_type = 'J';
  225. #else
  226.     if (c.g.special == 'J') {
  227.         c.g.screen_type = 'M';
  228.         c.g.screen_size = 'J';
  229.         c.g.key_type = 'J';
  230.         }
  231. #endif
  232. #endif
  233.  
  234.     WFixup(&c.d);
  235.     return(TRUE);
  236.     }
  237.  
  238.  
  239. /* ------------------------------------------------------------ */
  240.  
  241. /* Read and return the next character. */
  242.  
  243. int
  244. Ini_Next()
  245.     {
  246.     if (bufptr >= &buffer[buflen]) {
  247.         buflen = read(fd, buffer, sizeof(buffer));
  248.         if (buflen <= 0) return(EOF);
  249.         bufptr = buffer;
  250.         }
  251.     if (*bufptr == NL) line++;
  252.     return(*bufptr++ & 0xFF);
  253.     }
  254.  
  255.  
  256. /* ------------------------------------------------------------ */
  257.  
  258. /* Read and return one token. */
  259.  
  260. FLAG
  261. Ini_Token(token, len)
  262.     char *token;
  263.     int len;
  264.     {
  265.     int c;
  266.  
  267. /* skip whitespace */
  268.  
  269.     do    {
  270.         c = Ini_Next();
  271.         if (c == '#') {
  272.             do    {
  273.                 c = Ini_Next();
  274.                 } while (c != EOF && c != NL);
  275.             }
  276.         } while (xisgray(c));
  277.  
  278. /* copy token */
  279.  
  280.     while (--len > 0 && !xisgray(c)) {
  281.         *token++ = c;
  282.         c = Ini_Next();
  283.         }
  284.     *token = NUL;
  285.  
  286. /* check for trailing comment */
  287.  
  288.     if (c == '#') {
  289.         do    {
  290.             c = Ini_Next();
  291.             } while (c != EOF && c != NL);
  292.         }
  293.     return(c != EOF);
  294.     }
  295.  
  296.  
  297. /* end of INI.C -- Routines to Load the Initialization File */
  298.